<template>
  <z-modal
    :width="800"
    v-bind="modalOption"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="编码" prop="code">
              <a-input placeholder="请输入编码" :disabled="editType !== 'add' || disableSubmit" v-model.trim="form.code"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="名称" prop="name">
              <a-input placeholder="请输入名称" :disabled="editType !== 'add' || disableSubmit" v-model.trim="form.name"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="发布状态">
               <z-dict-select-tag v-model="form.status" :defaultValue="true" disabled dictCode="MB_FLOW_DEPLOY_STATUS"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </z-modal>
</template>

<script>
import ZDictSelectTag from '@/components/dict/ZDictSelectTag'
import ModalFormMixin from '@/mixins/ModalFormMixin'
import { duplicateCheck } from '@/services/common'
import debounce from 'lodash/debounce'
export default {
  name: 'BpmnModal',
  components: { ZDictSelectTag },
  mixins: [ModalFormMixin],
  data() {
    return {
      url: {
        add: '/camunda/flow/add',
        edit: '/camunda/flow/edit'
      },
      validatorRules: {
        code: [
          { required: true, message: '请输入编码' },
          { pattern: /^(?=.*[a-zA-Z])[a-zA-Z0-9]+$/, message: '编码必须包含英文字母，且不能包含特殊字符' },
          { validator: (rule, value, callback) => this.validateCode(rule, value, callback), trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入名称' }]
      }
    }
  },
  methods: {
    validateCode: debounce(function(rule, value, callback) {
      if (!value) {
        callback()
      } else {
        duplicateCheck({
          tableName: 'mb_bpmn_flow',
          column: 'code_',
          key: value,
          exclude: this.editType === 'edit' ? value : undefined
        }).then(res => {
          if (res.success) {
            res.result === 0 ? callback() : callback('编码已存在')
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    }, 400)
  }
}
</script>
